package com.richie696.dao;

import com.richie696.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;
import java.util.Set;

@CacheNamespace(implementation= RedisCache.class)
public interface UserDAO {

    @Select("select * from user_info")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "name", column = "name"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "orders", column="id", javaType = Set.class,
                many = @Many(fetchType = FetchType.LAZY, select="com.richie696.dao.OrderDAO.findOrdersByUserId")),
            @Result(property = "roles", column = "id", javaType = Set.class,
                many = @Many(select = "com.richie696.dao.RoleDAO.findRolesByUserId"))
    })
    List<UserInfo> findAll();

    @Select("select * from user_info where id = #{id}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "name", column = "name"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "orders", column="id", javaType = Set.class,
                    many = @Many(select="com.richie696.dao.OrderDAO.findOrdersByUserId", fetchType = FetchType.LAZY)),
            @Result(property = "roles", column = "id", javaType = Set.class,
                    many = @Many(select = "com.richie696.dao.RoleDAO.findRolesByUserId"))
    })
    UserInfo findUserById(Long id);

    @Select("select * from user_info where 1 = 1")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "name", column = "name"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "orders", column="id", javaType = Set.class,
                    many = @Many(select="com.richie696.dao.OrderDAO.findOrdersByUserId")),
            @Result(property = "roles", column = "id", javaType = Set.class,
                    many = @Many(select = "com.richie696.dao.RoleDAO.findRolesByUserId"))
    })
    List<UserInfo> findUsers(UserInfo userInfo);

}
